package com.augurit.common.syslog.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.dao.SysPropAuditLogDao;
import com.augurit.common.entity.SysPropAuditLog;
import com.augurit.common.syslog.convert.SysPropAuditLogConverter;
import com.augurit.common.syslog.service.ISysPropAuditLogService;
import com.augurit.common.syslog.tool.LogUtils;
import com.augurit.common.syslog.web.form.SysPropAuditLogForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;

@Service
@Transactional
public class SysPropAuditLogServiceImpl implements ISysPropAuditLogService{

	@Autowired
	private SysPropAuditLogDao sysLogPropAuditDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public SysPropAuditLogForm get(Long id) {
		SysPropAuditLog entity = sysLogPropAuditDao.get(id);
		return SysPropAuditLogConverter.convertVoToForm(entity);
	}

	/**
	 * 获取某个VO对象的字段变更记录
	 * @param entity VO对象名
	 * @param entityId VO对象主键
	 * @param orderByClause 排序HQL子句
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<SysPropAuditLogForm> getLogList(String entity, String entityId, String orderByClause){
		if(entity != null && entityId != null && orderByClause != null){
			String hql = "from SysPropAuditLog ps where ps.entity=? and ps.entityId=? " + orderByClause;
			List<SysPropAuditLog> list = sysLogPropAuditDao.find(hql, entity, entityId);
			return SysPropAuditLogConverter.convertVoListToFormList(list);
		}else
			return null;
	}
	
	
	/**
	 * 分页获取某个VO对象的字段变更记录
	 * @param page 分页参数
	 * @param entity VO对象名
	 * @param entityId VO对象主键
	 * @param orderByClause 排序HQL子句
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<SysPropAuditLogForm> getLogListOrderByUpdatorAndTime(Page<SysPropAuditLogForm> page, String entity, String entityId, String orderByClause){
		if(entity != null && entityId != null){
			Page pg = PageUtils.newPageInstance(page);
			
			String hql = "from SysPropAuditLog ps where ps.entity=? and ps.entityId=? "
				+ ((orderByClause != null && orderByClause.trim().length() > 0) ? orderByClause : "order by updator, updateTime desc");
			List values = new ArrayList();
			values.add(entity);
			values.add(entityId);
			pg = sysLogPropAuditDao.findPage(pg, hql, values);
			
			List<SysPropAuditLogForm> result = SysPropAuditLogConverter.convertVoListToFormList(pg.getResult());
			PageUtils.copy(pg, result, page);
			return page;
		}else
			return null;
	}
	
	
	/**
	 * 获取某个VO对象的字段变更记录
	 * @param entity VO对象名
	 * @param entityId VO对象主键
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<SysPropAuditLogForm> getLogListOrderByUpdatorAndTime(String entity, String entityId){
		List<SysPropAuditLogForm> list= getLogList(entity, entityId, "order by updator, updateTime desc");
		
		if(list != null && list.size() > 0){
			//计算字段审计日志列表中 修改人 和 修改时间 所占的行高，便于在页面上分组显示
			LogUtils.calcUpdatorRowSpan(list);
			LogUtils.calcUpdateTimeRowSpan(list);
		}
		
		return list;
	}
	
	/**
	 * 获取某个VO对象的字段变更记录
	 * @param entity VO对象名
	 * @param entityId VO对象主键
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<SysPropAuditLogForm> getLogListOrderByPropertyCommentAndTime(String entity, String entityId){
		List<SysPropAuditLogForm> list= getLogList(entity, entityId, "order by propertyComment, updateTime desc");
		
		if(list != null && list.size() > 0){
			//计算字段审计日志列表中 修改属性 和 修改时间 所占的行高，便于在页面上分组显示
			LogUtils.calcPropertyCommentRowSpan(list);
			LogUtils.calcUpdateTimeRowSpan(list);
		}
		
		return list;
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		sysLogPropAuditDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(SysPropAuditLogForm... forms) {
		if(forms != null)
			for(SysPropAuditLogForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(SysPropAuditLogForm form){
		
		if(form != null){
			SysPropAuditLog entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = sysLogPropAuditDao.get(form.getId());
			}else{
				entity = new SysPropAuditLog();
			}
			
			//属性值转换
			SysPropAuditLogConverter.convertFormToVo(form, entity);
			
			//保存
			sysLogPropAuditDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<SysPropAuditLogForm> search(Page<SysPropAuditLogForm> page, SysPropAuditLogForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from SysPropAuditLog ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			if(form.getEntity() != null && form.getEntity().trim().length() > 0){
				hql.append(" and ps.entity like ?");
				values.add("%" + form.getEntity() + "%");
			}
			if(form.getEntityId() != null && form.getEntityId().trim().length() > 0){
				hql.append(" and ps.entityId like ?");
				values.add("%" + form.getEntityId() + "%");
			}
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		sysLogPropAuditDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysPropAuditLogForm> list = SysPropAuditLogConverter.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<SysPropAuditLogForm> search(Page<SysPropAuditLogForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<SysPropAuditLog> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		sysLogPropAuditDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<SysPropAuditLogForm> list = SysPropAuditLogConverter.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
}
